/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.schedule.ScheduleItem;
import ge.info.domain.schedule.SchedulePlace;
import ge.info.domain.schedule.ScheduleType;
import ge.ktapestry.pages.BasePageExt;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.IPage;
import org.apache.hivemind.util.PropertyUtils;

public abstract class ScheduleTypePage extends BasePageExt implements PageBeginRenderListener
{

    public static int SERVICE_ID = 10;

    public abstract ScheduleItem getScheduleItem();

    public abstract void setScheduleItem(ScheduleItem scheduleItem);

    public abstract SchedulePlace getSchedulePlace();

    public abstract void setSchedulePlace(SchedulePlace schedulePlace);

    @Persist("session")
    public abstract SchedulePlace getSchedulePlaceCurrent();

    public abstract void setSchedulePlaceCurrent(SchedulePlace schedulePlaceCurrent);

    public abstract int getIndex();

    public abstract void setIndex(int i);

    @Persist("session")
    public abstract ScheduleType getScheduleType();

    public abstract void setScheduleType(ScheduleType scheduleType);

    @Persist("session")
    public abstract ScheduleType getListScheduleType();

    public abstract void setListScheduleType(ScheduleType scheduleType);

    public abstract String getPosterCriteria();

    public abstract void setPosterCriteria(String s);

   // @Persist("session")
    
    public abstract Date getScDate();

    public abstract void setScDate(Date date);

    public void pageBeginRender(PageEvent event)
    {

//        if (!event.getRequestCycle().isRewinding())
//        {
//            if (getListScheduleType() == null)
//            {
//                ScheduleType st = new ScheduleType();
//                st.setScheduleTypeID(-1);
//                st.setScheduleName(" ");
//                st.setServiceID(-1);
//                setListScheduleType(st);
//            //    setScheduleType((ScheduleType) getComboes().getCombo("ScheduleTypes").getOption(0));
//            // setScheduleType(getDbExecutor().populateSchedulePlaces(getScheduleType()));
//            }
//        }
    }

    public void getSchedule(SchedulePlace st)
    {
        SchedulePlace sp = new SchedulePlace();
        sp.setSchedulePlaceID(st.getSchedulePlaceID());
        sp.setPlaceName(st.getPlaceName());
        sp.setMainID(st.getMainID());
        setSchedulePlaceCurrent(sp);
        getSchedulePlaceCurrent().setScheduleItems(getDbExecutor().getScheduleItems(getSchedulePlaceCurrent(), getScheduleType()));

    }

     public void openMainResultPage(IRequestCycle cycle, Integer orgID)
    {
        IPage page = cycle.getPage("OrgResultPage");
        PropertyUtils.write(page, "organizationID", orgID);
        PropertyUtils.write(page, "organization", null);
        cycle.activate(page);
    }

      public void openDiscoverPage(IRequestCycle cycle, ScheduleItem schedule)
    {
          String curDate = schedule.getItemDate().substring(0, schedule.getItemDate().length());

        StringBuilder sb = new StringBuilder();
        sb.append(getSchedulePlaceCurrent().getPlaceName()).append(" ");
        if (schedule != null)
        {

            for (ScheduleItem it : getSchedulePlaceCurrent().getScheduleItems())
            {
                if (it.getItemDate().substring(0, schedule.getItemDate().length()).equals(curDate))
                {

                    sb.append(it.getItemName()).append(" ");
                    sb.append(it.getItemDate()).append(" ");
                    sb.append(it.getPrice()).append(" ");
                }
            }

          String dtxt = sb.toString();
          
        IPage page = cycle.getPage("DiscoverPage");
        PropertyUtils.write(page, "dtxt", dtxt);
        PropertyUtils.write(page, "discover", null);
        cycle.activate(page);
    }
      }

    public void searchPosters(IRequestCycle cycle)
    {
        setSchedulePlaceCurrent(null);



        if (getPosterCriteria() == null && getScDate() == null)
            // || getScDate() == null)
        {
            if (getListScheduleType().getScheduleTypeID() != -1)
            {
                setScheduleType(getDbExecutor().populateSchedulePlaces(getListScheduleType()));
            } else
            {
                setScheduleType(null);
            }
            return;
        }

         

        setScheduleType(new ScheduleType());
        getScheduleType().setScheduleTypeID(getListScheduleType().getScheduleTypeID());
        getScheduleType().setPosterCriteria(getPosterCriteria());

       if (getScDate() != null)

         {
            getScheduleType().setSdate(getScDate());
         }

       

        if (getListScheduleType().getScheduleTypeID() != -1)
        {

            setScheduleType(getDbExecutor().searchPosters(getScheduleType()));
        } else
        {
            List<ScheduleType> list = getDbExecutor().searchAllPosters(getScheduleType());
            ScheduleType schedule = new ScheduleType();
            ArrayList<SchedulePlace> places = new ArrayList<SchedulePlace>();
            
            schedule.setSchedulePlaces(places);
            for (ScheduleType st : list)
            {
                for (SchedulePlace place : st.getSchedulePlaces())
                {
                    places.add(place);
                }
            }
            setScheduleType(schedule);
        }
    }

    public boolean isBreak()
    {
        if (getSchedulePlaceCurrent() == null || getSchedulePlaceCurrent().getScheduleItems() == null || getIndex() == (getSchedulePlaceCurrent().getScheduleItems().size() - 1))
        {
            return false;
        }
        return !getSchedulePlaceCurrent().getScheduleItems().get(getIndex()).getItemDate().substring(0, 2).equals(getSchedulePlaceCurrent().getScheduleItems().get(getIndex() + 1).getItemDate().substring(0, 2));
    }

    public void sendSMS(IRequestCycle cycle, ScheduleItem schedule)
    {


        String curDate = schedule.getItemDate().substring(0,schedule.getItemDate().length());

        StringBuilder sb = new StringBuilder();
        sb.append(getSchedulePlaceCurrent().getPlaceName()).append(" ");
        if (schedule != null)
        {

            for (ScheduleItem it : getSchedulePlaceCurrent().getScheduleItems())
            {
                if (it.getItemDate().substring(0, schedule.getItemDate().length()).equals(curDate))
                {

                    sb.append(it.getItemName()).append(" ");
                    sb.append(it.getItemDate()).append(" ");
                    sb.append(it.getPrice()).append(" ");
                }
            }

            getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
        }


    }
    public void sendSMSday(IRequestCycle cycle, ScheduleItem schedule)
    {


        String curDate = schedule.getItemDate().substring(0,2);

        StringBuilder sb = new StringBuilder();
        sb.append(getSchedulePlaceCurrent().getPlaceName()).append(" ");
        if (schedule != null)
        {

            for (ScheduleItem it : getSchedulePlaceCurrent().getScheduleItems())
            {
                if (it.getItemDate().substring(0,2).equals(curDate))
                {

                    sb.append(it.getItemName()).append(" ");
                    sb.append(it.getItemDate()).append(" ");
                    sb.append(it.getPrice()).append(" ");
                }
            }

            getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
        }


    }
}
